package com.foreknow.elm.dao.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Food;
import com.foreknow.elm.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 啊滨
 * @date 2023/2/14 16:53
 */
public class CartDaoImpl implements CartDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;
    @Override
    public List<Cart> listCart(String userId,Integer businessId) throws Exception {
        List<Cart> list=new ArrayList<>();
        //StringBuffer sql 创建动态sql
        StringBuffer sql = new StringBuffer("\n" +
                "SELECT c.foodId,f.foodName,f.foodExplain,f.foodImg,f.foodPrice,f.remarks,c.businessId,b.businessName,b.businessAddress,b.businessExplain,b.businessImg,b.orderTypeId,b.deliveryPrice,b.remarks,c.quantity\n" +
                "FROM cart c,food f,business b\n" +
                "WHERE c.foodId=f.foodId \n" +
                "AND c.businessId = b.businessId");
        if (businessId != null && userId!= null) {
            // sql.append添加sql
            sql.append(" AND c.userId =? AND c.businessId =?");
        }else if (userId != null) {
            sql.append(" AND c.userId =?");
        }else {
            sql.append(" AND c.businessId =?");
        }
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(String.valueOf(sql));
            if (businessId != null && userId!= null) {
                pst.setString(1, userId);
                pst.setInt(2, businessId);
            }else if (userId != null) {
                pst.setString(1, userId);
            }else {
                pst.setInt(1, businessId);
            }
            rs = pst.executeQuery();
            while (rs.next()){
                Cart cart=new Cart();
                Food food=new Food();
                Business business=new Business();
                cart.setQuantity(rs.getInt("quantity"));
                cart.setFoodId(rs.getInt("foodId"));
                cart.setBusinessId(rs.getInt("businessId"));
                food.setFoodName(rs.getString("foodName"));
                food.setFoodExplain(rs.getString("foodExplain"));
                food.setFoodImg(rs.getString("foodImg"));
                food.setFoodPrice(rs.getDouble("foodPrice"));
                food.setRemarks(rs.getString("remarks"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                cart.setBusiness(business);
                cart.setFood(food);
                list.add(cart);
            }
        }finally {
            DBUtil.close(rs,pst);
        }
        return list;
    }

    @Override
    public int saveCart(String userId, Integer businessId, Integer foodId) throws Exception{
        int isRight=0;
        String sql="INSERT INTO cart(userId,businessId,foodId,quantity) VALUES(?,?,?,1)";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            pst.setInt(3,foodId);
            isRight=pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return isRight;
    }

    @Override
    public int updateCart(String userId, Integer businessId, Integer foodId, Integer quantity) throws Exception {
        int isRight=0;
        String sql="UPDATE cart SET quantity=? WHERE userId=? and businessId=? and foodId=?";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setInt(1,quantity);
            pst.setString(2,userId);
            pst.setInt(3,businessId);
            pst.setInt(4,foodId);
            isRight=pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return isRight;
    }

    @Override
    public int removeCart(String userId, Integer businessId, Integer foodId) throws Exception {
        int isRight=0;
        StringBuffer sql = new StringBuffer("DELETE FROM cart WHERE userId=? AND businessId=? ");
        if (foodId!=null){
            sql.append("AND foodId=?");
        }
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(String.valueOf(sql));
            pst.setString(1,userId);
            pst.setInt(2,businessId);
            if (foodId!=null){
                pst.setInt(3,foodId);
            }
            isRight=pst.executeUpdate();
        }finally {
            DBUtil.close(rs,pst);
        }
        return isRight;
    }
}
